package com.modifiertech.lineage.effect;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.commands.CmdHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.potion.Potion;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.LivingKnockBackEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

/**
 * @description: default
 * @author: saliencemn
 * @mail: hackingmu@outlook.com
 * @date: 2024/8/26 22:50
 */
public class EffectStun extends Potion {
    // 第一个参数是是否为负面效果，第二个参数是颜色
    protected EffectStun() {
        super(true, 0x98D982);
        // 确保 setPotionName 仅在客户端执行
        if (FMLCommonHandler.instance().getSide().isClient()) {
            setPotionName("effect.stun");
        }
        setRegistryName(new ResourceLocation(BaseConst.MODID, "stun"));
    }

    @Override
    public void performEffect(EntityLivingBase entity, int amplifier) {
        // 在这里可以添加每 tick 的效果
        // 禁止移动
//        entity.setVelocity(0, 0, 0); // 确保速度为零
//        entity.motionX = 0;
//        entity.motionY = 0;
//        entity.motionZ = 0;
//        entity.setPosition(entity.prevPosX, entity.prevPosY, entity.prevPosZ);
//            entity.limbSwingAmount = 0;
//            entity.setSprinting(false);
//            entity.setLastAttackedEntity(null);
//            // 停止路径导航和跳跃
//            if (entity instanceof EntityCreature) {
//                ((EntityCreature) entity).getNavigator().clearPath();
//            }
//            entity.setJumping(false);

    }

    @Override
    public boolean isReady(int duration, int amplifier) {
        return true;
    }

    @SubscribeEvent
    public static void onLivingUpdate(LivingEvent.LivingUpdateEvent event) {
        EntityLivingBase entity = event.getEntityLiving();
        if (entity.isPotionActive(EffectRegister.STUN_POTION)) {
            // 禁止移动
            entity.motionX = 0;
            entity.motionY = 0;
            entity.motionZ = 0;
            entity.velocityChanged = true;

            // 禁止攻击和其他动作
            entity.limbSwingAmount = 0;
            entity.setSprinting(false);
            //
            if (Math.abs(entity.posX-entity.prevPosX)<2
                    && Math.abs(entity.posY-entity.prevPosY)<2
                    && Math.abs(entity.posZ-entity.prevPosZ)<2){
                entity.setPosition(entity.prevPosX,entity.prevPosY,entity.prevPosZ);
            }

            // 停止路径导航和跳跃
            if (entity instanceof EntityCreature) {
                ((EntityCreature) entity).getNavigator().clearPath();
            }
            entity.setJumping(false);
        }
    }

    @SubscribeEvent
    public static void onLivingAttack(LivingAttackEvent event) {
        Entity attacker = event.getSource().getTrueSource();
        if (event.getEntityLiving() instanceof EntityPlayer){
            if (attacker instanceof EntityLivingBase) {
                EntityLivingBase entity = (EntityLivingBase) attacker;
                // 检查受击者是否是玩家，并且攻击者是否受到特定药水效果影响
                if (entity.isPotionActive(EffectRegister.STUN_POTION)) {
                    // 取消attack事件
                    event.setCanceled(true);
                }
            }
        }
    }
    @SubscribeEvent
    public static void onLivingKnockBack(LivingKnockBackEvent event) {
        // 检查攻击方是否存在并且是 EntityLivingBase 类型
        Entity attacker = event.getAttacker();
        if (attacker instanceof EntityLivingBase) {
            EntityLivingBase entity = (EntityLivingBase) attacker;

            // 检查受击者是否是玩家，并且攻击者是否受到特定药水效果影响
            if (event.getEntityLiving() instanceof EntityPlayer && entity.isPotionActive(EffectRegister.STUN_POTION)) {
                // 取消击退效果
                event.setCanceled(true);
            }
        }



    }


}
